package net.zhengxinyun.performance.controller.login;


import net.zhengxinyun.performance.core.Result;
import net.zhengxinyun.performance.core.ResultUtils;
import net.zhengxinyun.performance.entity.TokenEntity;
import net.zhengxinyun.performance.entity.UserEntity;
import net.zhengxinyun.performance.filter.CheckFilter;
import net.zhengxinyun.performance.filter.DepartmentFilter;
import net.zhengxinyun.performance.filter.UserFilter;
import net.zhengxinyun.performance.filter.WarnFilter;
import net.zhengxinyun.performance.service.Check.CheckService;
import net.zhengxinyun.performance.service.department.DepartmentService;
import net.zhengxinyun.performance.service.login.LoginService;
import net.zhengxinyun.performance.service.login.TokenService;
import net.zhengxinyun.performance.service.login.UserService;
import net.zhengxinyun.performance.service.warn.WarnService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: hyp
 * @Date: 2018/10/30 14:47
 */
@RestController
@RequestMapping("/Login")
public class LoginController {

    private LoginService loginService;
    private TokenService tokenService;
    private UserService userService;
    private CheckService checkService;
    private DepartmentService departmentService;
    private WarnService warnService;

    @Autowired
    public LoginController(LoginService loginService, TokenService tokenService, UserService userService, CheckService checkService, DepartmentService departmentService, WarnService warnService) {
        this.loginService = loginService;
        this.tokenService = tokenService;
        this.userService = userService;
        this.checkService = checkService;
        this.departmentService = departmentService;
        this.warnService = warnService;
    }

    @PostMapping("/loginGetToken")
    public Result<String> loginGetToken(@RequestBody UserFilter userFilter) {
        TokenEntity token = null;
        try {
            token = loginService.loginGetToken(userFilter);
        } catch (Exception e) {

        }
        if (token == null){
            return ResultUtils.getResults(Result.Status.ERROR,"用户名密码错误");
        }else {
            return ResultUtils.getResults(Result.Status.SUCCESS,token.getValue());
        }
    }

    @PostMapping("/loginUserInfo")
    public Result<HashMap<String, Object>> loginGetUserInfo(@RequestBody TokenEntity tokenEntity){
        if (tokenEntity == null){
            return ResultUtils.getResults(Result.Status.OVERTIME,"没有token",null);
        }
        UserEntity userEntity = loginService.getUserByToken(tokenEntity);
        if (userEntity == null){
            return ResultUtils.getResults(Result.Status.OVERTIME,"没有token",null);
        }
        List<String> departmentList = departmentService.selectForDepartment();
        HashMap<String,Object> userMap = new HashMap<>();
        String[] role = new String[]{userEntity.getUserPermissions()};
        //查看用户是否有需要提醒的查访核验项目
        if (!"admin".equals(userEntity.getUserPermissions())){
            CheckFilter checkFilter = new CheckFilter();
            checkFilter.setAddressee(userEntity.getUserDepartment());
            checkFilter.setStatus("0");
            List<CheckFilter> list = checkService.query(checkFilter,userEntity);
            userMap.put("emailWarning",list.size());
        }
        //如果是经办人判断是否有需要预警的指标
        if ("agent".equals(userEntity.getUserPermissions())){
            List<WarnFilter> list = warnService.loginQuery(userEntity);
            userMap.put("warn",list);
        }
        userMap.put("name",userEntity.getUserName());//用户账户
        userMap.put("user_id",userEntity.getId());//用户id
        userMap.put("access",role);//用户权限
        userMap.put("department",userEntity.getUserDepartment());
        userMap.put("token",tokenEntity.getValue());
        userMap.put("departmentList",departmentList);
        if ("admin".equals(userEntity.getUserPermissions())){
            userMap.put("role","管理员");
        }else if ("responsible".equals(userEntity.getUserPermissions())){
            userMap.put("role","责任人");
        }else if ("agent".equals(userEntity.getUserPermissions())){
            userMap.put("role","经办人");
        }
        return ResultUtils.getSuccessResults(userMap);
    }

    @PostMapping("/exit")
    public Result<String> exit(@RequestBody TokenEntity tokenEntity){
        if (tokenService.compareToken(tokenEntity.getValue())){
            tokenService.delete(tokenEntity);
            return ResultUtils.getSuccessResults();
        }else {
            return ResultUtils.getResults(Result.Status.OVERTIME);
        }
    }
}

